﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace hk6
{
    public partial class Form1 : Form
    {
        List<string> pathList;
        int index = 0;
        int selectedIndex = 0;
        List<bool> unsave;
        public Form1()
        {
            
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            pathList = new List<string>();
            unsave = new List<bool>();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            toolStripTextBox2.PerformClick();
            BindListToListView1(pathList);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedIndices.Count > 0)
            {
                selectedIndex = listView1.SelectedIndices[0];
                pathList.RemoveAt(selectedIndex);
                listView1.Items.RemoveAt(selectedIndex);
                unsave.RemoveAt(selectedIndex);
                BindListToListView1(pathList);
            }
        }

        

        private void BindListToListView1(List<string> list)
        {
            listView1.Items.Clear();  // 清除 ListView 中的所有项

            foreach (string item in list)
            {
                if (item != "NoPath")
                {
                    string itemName = Path.GetFileName(item);
                    ListViewItem listViewItem = new ListViewItem(itemName);
                    listView1.Items.Add(listViewItem);
                }
                else
                {
                    string itemName = "NewItem .txt";
                    ListViewItem listViewItem = new ListViewItem(itemName);
                    listView1.Items.Add(listViewItem);
                }
            }
        }

        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (listView1.SelectedIndices.Count > 0)
            {
                selectedIndex = listView1.SelectedIndices[0];
            }
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listView1.SelectedIndices.Count > 0)
            {
                selectedIndex = listView1.SelectedIndices[0];
            }
            string filePath = pathList[selectedIndex];
            try
            {
                using (StreamReader sr = new StreamReader(filePath))
                {
                    string textFromTxt = sr.ReadToEnd();
                    textBox1.Text = textFromTxt;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error reading file: {ex.Message}");
            }
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void createToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pathList.Add("NoPath");
            unsave.Add(false);
            BindListToListView1(pathList);
            toolStripMenuItem1.HideDropDown();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = @"C:\";
            openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog.FilterIndex = 1;
            openFileDialog.RestoreDirectory = true;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string selectedFilePath = openFileDialog.FileName;
                pathList.Add(selectedFilePath);
                unsave.Add(false);

            }
            BindListToListView1(pathList);
            toolStripMenuItem1.HideDropDown();
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void toolStripTextBox1_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedIndices.Count > 0)
            {
                string filePath = pathList[selectedIndex];
                if (filePath != "NoPath")
                {
                    try
                    {

                        using (StreamWriter sw = new StreamWriter(filePath))
                        {
                            sw.Write(textBox1.Text);
                        }

                        MessageBox.Show("Save succeed!");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"Error saving file: {ex.Message}");
                    }
                }
                else
                {

                    using (SaveFileDialog saveFileDialog = new SaveFileDialog())
                    {

                        saveFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
                        saveFileDialog.InitialDirectory = @"C:\";

                        if (saveFileDialog.ShowDialog() == DialogResult.OK)
                        {
                            filePath = saveFileDialog.FileName;
                            try
                            {

                                using (StreamWriter sw = new StreamWriter(filePath))
                                {
                                    sw.Write(textBox1.Text);
                                }

                                MessageBox.Show("Save succeed!");
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show($"Error saving file: {ex.Message}");
                            }
                        }
                    }
                }

            }
            if (listView1.Items[selectedIndex].Text[listView1.Items[selectedIndex].Text.Length - 1] == '*' && unsave[selectedIndex] == true)
            {
                unsave[selectedIndex] = false;
                listView1.Items[selectedIndex].Text.Remove(listView1.Items[selectedIndex].Text.Length - 1);
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            label2.Text = Convert.ToString(textBox1.Text.Length);
            ListViewItem listViewItem = listView1.SelectedItems[0];
            if (unsave[selectedIndex] == false)
            {
                listViewItem.Text += "*";
                unsave[selectedIndex] = true;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            listView1.Size = new System.Drawing.Size(1080, 497);
            listView1.View = View.Tile;
            textBox1.Visible = false;
            textBox1.Enabled = false;
            panel1.Visible = false;
           

            button4.Visible = false;
           
            button4.Enabled = false;

            button5.Visible = true;
            button5.Enabled = true;
            label2.Visible = false;
            label2.Visible = false;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            listView1.Size = new System.Drawing.Size(347,497);
            textBox1.Location = new System.Drawing.Point(380,105);
            textBox1.Size = new System.Drawing.Size(728,497);
            listView1.View = View.List;
            textBox1.Visible = true;
            textBox1.Enabled = true;
            panel1.Visible = true;
            button1.Visible = true;
            button2.Visible = true;
            button4.Visible = true;
            button4.Enabled = true;
            button1.Enabled = true;
            button2.Enabled = true;
            button5.Visible = false;
            button5.Enabled = false;
            label2.Visible = true;
            label2.Visible = true;
            listView1.Visible = true;
            listView1.Enabled = true;
        }
    }
}
